#Requires AutoHotkey v2.0
flashCards(&txt) {
  static getEditText    ;输入的单词
  static edit := 0      ;翻译完成的单词
  static obj := {}      ;英文词典的翻译结果
  resetEdit() {
    edit := 0
  }

  SetTimer(resetEdit, 0)        ;取消重置翻译完成的flag
  SetTimer(OnlyInput.automaticCompletion, 0)    

  ; OnlyInput.getCtrText := getEditText := Trim(ControlGetText("Edit1"))            ;主动给监听比对词赋值,防止执行自动补全
  getEditText := txt
  src := "E:\Desktop\obsidian\ahkFile\fileAppend\flashCards" . A_Year . A_Mon . ".md"   ;写入的obsidian的md格式文件的地址
  
  if (getEditText) {
    if (edit = getEditText) {      ;已翻译完,则写入文件
      content := "`n`n" . edit . "`t" . obj.audio . "`n??" . "`n- " . OnlyInput.parameter . obj.content . "`n`n"    ;使用spaced repetition插件flash-card规范的格式写入
      FileAppend(content, src, "UTF-8") 
      ControlSetText("","Edit1")
      OnlyInput.searchingHistory.Push(edit) 
      OnlyInput.historyIndex := OnlyInput.searchingHistory.Length + 1
      edit := 0
    } else {                       ;未翻译,则翻译单词
      OnlyInput.parameter := Translate(&getEditText)    ;调用百度翻译
      OnlyInput.fixedTip(OnlyInput.parameter)      
      obj := dictionary(getEditText)           ;调用英文词典翻译
      if (WinActive("ahk_group OnlyInput")) {
        OnlyInput.fixedTip(OnlyInput.parameter . obj.text)    ;显示翻译结果
        try{
          SoundPlay(obj.sound)         ;播放音频
        }
      }
      edit := getEditText              ;翻译完成的flag
      SetTimer(resetEdit, 3000)        ;等待三秒后,会清除翻译完成的flag,需要重新翻译,才能写入,
    }
  } else {
    (WinActive(OnlyInput1)) and OnlyInput1.Hide()
    (WinActive(OnlyInput2)) and OnlyInput2.Hide()
    (WinActive(OnlyInput3)) and OnlyInput3.Hide()
    mediaPlayPause()           ;视频暂停
  }
}
